package com.zsxj.xixi.common.utils;

import org.springframework.lang.Nullable;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import static java.math.RoundingMode.HALF_UP;

/**
 * @Description 数值处理相关工具类
 * @Author zhengbaole
 * @Date 2020/11/26
 **/
public class NumberUtils {

    public static final int MONEY_POINT = 2; // 货币保留两位小数
    private static final int DECIMAL_SCALE = 2;


    private NumberUtils() {
    }

    /**
     * @return 判断对象类型并转换为 Long
     *
     * @author zhengbaole
     * @lastEditor zhengbaole
     * @createTime 2020/11/26 2:48 下午
     * @editTime 2020/11/26 2:48 下午
     **/
    @Nullable
    public static Long convertToLong(Object value) {
        if (value == null) {
            return null;
        }

        Long longValue = null;

        if (value instanceof Integer) {
            longValue = ((Integer) value).longValue();
        } else if (value instanceof Long) {
            longValue = (Long) value;
        } else if (value instanceof BigDecimal) {
            longValue = ((BigDecimal) value).longValue();
        }

        return longValue;
    }

    /**
     * 如果对象小于0，返回0
     *
     * @author liuyuqing
     * @lastEditor liuyuqing
     * @createTime 2020/12/21 13:29
     * @editTime 2020/12/21 13:29
     **/
    public static BigDecimal negativeToZero(BigDecimal value) {
        if (value == null) {
            return null;
        }
        if (isNegative(value)) {
            return BigDecimal.ZERO;
        }
        return value;
    }

    /**
     * 判断对象是否为负数
     *
     * @author liuyuqing
     * @lastEditor liuyuqing
     * @createTime 2020/12/21 13:30
     * @editTime 2020/12/21 13:30
     **/
    public static boolean isNegative(BigDecimal value) {
        if (value == null) {
            return false;
        }
        return value.compareTo(BigDecimal.ZERO) < 0;
    }

    /**
     * @return 是否正数
     *
     * @author zhengbaole
     * @lastEditor zhengbaole
     * @createTime 2020/12/22 4:36 下午
     * @editTime 2020/12/22 4:36 下午
     **/
    public static boolean isPositive(BigDecimal value) {
        if (value == null) {
            return false;
        }
        return 0 < value.compareTo(BigDecimal.ZERO);
    }

    /**
     * @return 是否正数
     *
     * @author zhengbaole
     * @lastEditor zhengbaole
     * @createTime 2020/12/22 4:36 下午
     * @editTime 2020/12/22 4:36 下午
     **/
    public static boolean isPositive(Long value) {
        if (value == null) {
            return false;
        }
        return 0 < value.compareTo(0L);
    }

    /**
     * @return 是否正数
     *
     * @author zhengbaole
     * @lastEditor zhengbaole
     * @createTime 2021/3/23 2:16 PM
     * @editTime 2021/3/23 2:16 PM
     */
    public static boolean isPositive(Integer value) {
        if (value == null) {
            return false;
        }
        return 0 < value.compareTo(0);
    }


    /**
     * 格式化精度
     *
     * @param v
     * @param point
     *            小数位数
     * @return double
     */
    public static Double format(double v, int point) {
        BigDecimal b = new BigDecimal(v);
        return b.setScale(point, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     *
     * @param v
     * @param point
     * @return
     */
    public static Double formatRoundUp(double v, int point) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setRoundingMode(RoundingMode.HALF_UP);//设置四舍五入
        nf.setMinimumFractionDigits(point);//设置最小保留几位小数
        nf.setMaximumFractionDigits(point);//设置最大保留几位小数
        return Double.valueOf(nf.format(v));
    }

    /**
     * 格式化金额。带千位符
     *
     * @param v
     * @return
     */
    public static String moneyFormat(Double v) {
        DecimalFormat formater = new DecimalFormat();
        formater.setMaximumFractionDigits(2);
        formater.setGroupingSize(3);
        formater.setRoundingMode(RoundingMode.FLOOR);
        return formater.format(v.doubleValue());
    }

    /**
     * 带小数的显示小数。不带小数的显示整数
     * @param d
     * @return
     */
    public static String doubleTrans(Double d) {
        if (Math.round(d) - d == 0) {
            return String.valueOf((long) d.doubleValue());
        }
        return String.valueOf(d);
    }

    /**
     * BigDecimal 相加
     *
     * @param v1
     * @param v2
     * @return double
     */
    public static Double add(double v1, double v2) {
        BigDecimal n1 = new BigDecimal(Double.toString(v1));
        BigDecimal n2 = new BigDecimal(Double.toString(v2));
        return n1.add(n2).doubleValue();
    }

    /**
     * BigDecimal 相加
     *
     * @param v1
     * @param v2
     * @return double
     */
    public static Long add(long v1, long v2) {
        BigDecimal n1 = new BigDecimal(Long.toString(v1));
        BigDecimal n2 = new BigDecimal(Long.toString(v2));
        return n1.add(n2).longValue();
    }

    /**
     * BigDecimal 相减法
     *
     * @param v1
     * @param v2
     * @return double
     */
    public static Long subtract(long v1, long v2) {
        BigDecimal n1 = new BigDecimal(Long.toString(v1));
        BigDecimal n2 = new BigDecimal(Long.toString(v2));
        return n1.subtract(n2).longValue();
    }


    /**
     * BigDecimal 相减
     *
     * @param v1
     * @param v2
     * @return double
     */
    public static Double subtract(double v1, double v2) {
        BigDecimal n1 = new BigDecimal(Double.toString(v1));
        BigDecimal n2 = new BigDecimal(Double.toString(v2));
        return n1.subtract(n2).doubleValue();
    }

    /**
     * BigDecimal 相乘
     *
     * @param v1
     * @param v2
     * @return double
     */
    public static Double multiply(double v1, double v2) {
        BigDecimal n1 = new BigDecimal(Double.toString(v1));
        BigDecimal n2 = new BigDecimal(Double.toString(v2));
        return n1.multiply(n2).doubleValue();
    }

    /**
     * BigDecimal 相除
     *
     * @param v1
     * @param v2
     * @return double
     */
    public static Double divide(double v1, double v2) {
        BigDecimal n1 = new BigDecimal(Double.toString(v1));
        BigDecimal n2 = new BigDecimal(Double.toString(v2));
        return n1.divide(n2, 10, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 比较大小 小于0：v1 < v2 大于0：v1 > v2 等于0：v1 = v2
     *
     * @param v1
     * @param v2
     * @return
     */
    public static int compare(double v1, double v2) {
        BigDecimal n1 = new BigDecimal(Double.toString(v1));
        BigDecimal n2 = new BigDecimal(Double.toString(v2));
        return n1.compareTo(n2);
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        // System.out.println(divide(1, 8));
        // System.out.println(format(multiply(3.55, 2.44),2));
        // System.out.println(divide(1.0, 3.0));
        // System.out.println(add(2.79, -3.0));
        System.out.println(doubleTrans(10000.0));
    }

    /**
     * 四舍五入到指定精度
     *
     * @param precision 精度
     */
    public static BigDecimal round(BigDecimal sourceNumber, int precision) {
        return sourceNumber.setScale(precision, HALF_UP);
    }

    /**
     * 四舍五入保留两位小数
     */
    public static BigDecimal round(BigDecimal sourceNumber) {
        return round(sourceNumber, DECIMAL_SCALE);
    }
}
